/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.swan.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

/**
 * Goal which touches a timestamp file.
 */
@Mojo(name = "config", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class ConfigFileMojo extends AbstractMojo {
    public static final String DEFAULT_CONFIG_JSON = "_default_config.json";
    @Parameter(defaultValue = "${project.basedir}/template", property = "dataPath", required = true)
    private File basedir;

    public void execute() throws MojoExecutionException, MojoFailureException {
        String content = "{\n" +
                "  \"Tbladdress\": {\n" +
                "    \"packageName\": \"com.mik.bean\"\n   },\n" +
                "  \".*package.*\": {\n" +
                "    \"packageName\": \"com.mik.pkg.bean\"\n  },\n" +
                "  \".*package2.*\": {\n" +
                "    \"packageName\": \"com.mik.pkg2.bean\"\n  }\n" +
                "}";
        File file = new File(basedir, DEFAULT_CONFIG_JSON);
        if (!file.exists()) {
            try {
                Files.writeString(file.toPath(), content, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
            } catch (IOException e) {
                getLog().error(e);
            }
        }
        file = new File(basedir, "readme.md");
        if (!file.exists()) {
            try {
                content = "Config File format is:\n" +
                        "\n" +
                        "```json\n" +
                        "{\n" +
                        "  \"Address\": {\n" +
                        "    \"packageName\": \"com.mik.bean\"\n  },\n" +
                        "  \".*package.*\": {\n" +
                        "    \"packageName\": \"com.mik.pkg.bean\"\n  },\n" +
                        "  \".*package2.*\": {\n" +
                        "    \"packageName\": \"com.mik.pkg2.bean\"\n  }\n" +
                        "}\n" +
                        "```\n" +
                        "\n" +
                        "There are multi KV items. Key is an regular expression use to match the id of data.\n" +
                        "\n" +
                        "When it matches the data, the attribute will set to default value in the data file if the file doesn't contain the attribute.";
                Files.writeString(file.toPath(), content, StandardCharsets.UTF_8, StandardOpenOption.CREATE);

                content = "{\n" +
                        "  \"template\": [\n" +
                        "    {\n" +
                        "      \"url\": \"src/main/resources/one.btl\",\n" +
                        "      \"targetUrl\": \"test/a.java\"\n" +
                        "    },\n" +
                        "    {\n" +
                        "      \"url\": \"src/main/resources/another.btl\",\n" +
                        "      \"targetUrl\": \"test/b.java\"\n" +
                        "    }\n" +
                        "  ],\n" +
                        "  \"typeBean\": {\n" +
                        "    \"mapKey\": {\n" +
                        "      \"clazz\": \"java.lang.String\"\n" +
                        "    },\n" +
                        "    \"mapValue\": {\n" +
                        "      \"clazz\": \"java.lang.String\"\n" +
                        "    }\n  },\n" +
                        "  \"data\": {\n" +
                        "    \"name\": \"wukong\",\n" +
                        "    \"skill\": 9999\n  }\n" +
                        "}";
                file = new File(basedir, "example.def.json");
                Files.writeString(file.toPath(), content, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
            } catch (IOException e) {
                getLog().error(e);
            }
        }
        getLog().info("Please modify the file " + DEFAULT_CONFIG_JSON + " in " + basedir.getAbsolutePath());
    }
}
